package org.vsaulyak.springrssagregator.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.RowMapper;
import org.vsaulyak.springrssagregator.entity.Item;
import org.vsaulyak.springrssagregator.exception.ChanelParsingException;

public class ItemDaoMySQL extends NamedParameterJdbcDaoSupport implements ItemDao
{
    private static final String PERSIST = "INSERT INTO items (`rank`, `link`, `title`, `chanel_id`) VALUES (:rank, :link, :title, :chanel_id)";
    private static final String GET = "SELECT `id`, `rank`, `link`, `title` FROM items WHERE `id` = :id";
    private static final String UPDATE = "UPDATE items SET `rank` = :rank, `link` = :link, `title` = :title WHERE id = :id";
    private static final String DELETE = "DELETE FROM items WHERE id = :id";
    
    @Override
    public void persistItem(Item item, long chanelId)
    {
        SqlParameterSource params = new MapSqlParameterSource()
                .addValue("rank", item.getRank())
                .addValue("link", item.getLink())
                .addValue("title", item.getTitle())
                .addValue("chanel_id", chanelId);
        
        KeyHolder key = new GeneratedKeyHolder();
        
        namedParameterJdbcTemplate.update(PERSIST, params, key);
        
        item.setId(key.getKey().longValue());
    }
    
    @Override
    public Item getItemById(long id)
    {
        SqlParameterSource params = new MapSqlParameterSource()
                .addValue("id", id);

        return namedParameterJdbcTemplate.queryForObject(GET, params, new
                RowMapper<Item>(){
                    @Override
                    public Item mapRow(ResultSet rs, int rowNum) throws SQLException {
                        Item item = new Item();
                        item.setId(rs.getLong("id"));
                        item.setRank(rs.getInt("rank"));
                        item.setLink(rs.getString("link"));
                        item.setTitle(rs.getString("title"));
                        return item;
                    }
                });
    }
    
    @Override
    public void updateItem(Item item)
    {
        SqlParameterSource params = new MapSqlParameterSource()
                .addValue("id", item.getId())
                .addValue("rank", item.getRank())
                .addValue("link", item.getLink())
                .addValue("title", item.getTitle());
        
        namedParameterJdbcTemplate.update(UPDATE, params);
    }
    
    @Override
    public void deleteItem(Item item)
    {
        SqlParameterSource params = new MapSqlParameterSource()
                .addValue("id", item.getId());
        
        namedParameterJdbcTemplate.update(DELETE, params);
    }
}
